package com.example.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.entity.ShangJia;
import com.example.backend.mapper.ShangJiaMapper;
import com.example.backend.service.IShangJiaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ShangJiaService implements IShangJiaService {
    @Autowired
    private ShangJiaMapper shangJiaMapper;

    @Override
    public boolean addShangJia(ShangJia shangJia) {
        return shangJiaMapper.insert(shangJia)>0;
    }

    @Override
    public boolean deleteShangJia(Integer sjid) {
        return shangJiaMapper.deleteById(sjid)>0;
    }

    @Override
    public boolean updateShangJia(ShangJia shangJia) {
        return shangJiaMapper.updateById(shangJia)>0;
    }

    @Override
    public List<ShangJia> queryShangJiaByName(String name) {
        if (name == null || name.isEmpty()) {
            return List.of();
        }
        return shangJiaMapper.selectList(new LambdaQueryWrapper<ShangJia>().like(ShangJia::getName, name));
    }

    @Override
    public List<ShangJia> queryShangJiaList() {
        return shangJiaMapper.selectList(null);
    }


    @Override
    public List<ShangJia> queryShangJia() {
        return shangJiaMapper.selectList(null);
    }

    @Override
    public ShangJia queryShangJiaById(Integer sjid) {
        return shangJiaMapper.selectById(sjid);
    }

    @Override
    public Page<ShangJia> queryShangJiaByPage(ShangJia shangJia,
                                      Integer page,
                                      Integer size,
                                      String sortField,
                                      String sortOrder) {
        // 构建查询条件
        LambdaQueryWrapper<ShangJia> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shangJia.getSjid() != null, ShangJia::getSjid, shangJia.getSjid())
                .like(StringUtils.hasText(shangJia.getName()), ShangJia::getName, shangJia.getName())
                .eq(StringUtils.hasText(shangJia.getAddress()), ShangJia::getAddress, shangJia.getAddress())
                .eq(shangJia.getStatus() != null, ShangJia::getStatus, shangJia.getStatus());

        // 应用排序
        if (sortField != null && !sortField.isEmpty()) {
            // 需要实现字段映射方法
            SFunction<ShangJia, ?> column = getColumnByField(sortField);
            if (column != null) {
                wrapper.orderBy(true, "asc".equalsIgnoreCase(sortOrder), column);
            }
        }

        // 执行分页查询
        return shangJiaMapper.selectPage(new Page<>(page, size), wrapper);
    }

    @Override
    public ShangJia queryShangJiaByYhid(Integer yhid) {
        return shangJiaMapper.queryShangJiaByYhid(yhid);
    }

    // 字段映射方法（根据实际字段调整）
    private SFunction<ShangJia, ?> getColumnByField(String field) {
        switch (field) {
            case "sjid":
                return ShangJia::getSjid;
            case "name":
                return ShangJia::getName;
            case "address":
                return ShangJia::getAddress;
            case "status":
                return ShangJia::getStatus;
            default:
                return null;
        }
    }
}
